package com.hyt.it.ogt.kq.service.gov.arrange;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.hyt.it.ogt.kq.common.gov.constant.ArrangeParamCodeEnum;
import com.hyt.it.ogt.kq.common.gov.constant.StringCodeEnum;
import com.hyt.it.ogt.kq.common.gov.utils.DateTimeUtil;
import com.hyt.it.ogt.kq.common.gov.utils.StringUtil;
import com.hyt.it.ogt.kq.service.gov.mapper.TimeRoomCandidateMapper;
import com.hyt.it.ogt.kq.service.gov.model.vo.TaskPlaceVO;

import cn.hutool.core.util.RandomUtil;

@Service
public class AdmissionService {
    public static final String ADMISSION_TYPE = "admissionType";
    @Resource
    private TimeRoomCandidateMapper timeRoomCandidateMapper;


    /**
     * 获取准考证号
     *
     * @param taskId
     * @param paramMap
     * @param taskPlaceVO
     * @return
     */
    public String getAdmissionNumber(String taskId, Map paramMap, TaskPlaceVO taskPlaceVO) {
        //获取准考证号生成方式
        int admissionType = Integer.parseInt(String.valueOf(paramMap.get(ADMISSION_TYPE)));
        // 组装准考证号
        if (admissionType == ArrangeParamCodeEnum.ADMISSION_TYPE_GROUP.getCode().intValue()) {
            //准考证方式-组合
            return getAdmissionGroup(taskId, paramMap, taskPlaceVO);
        } else if (admissionType == ArrangeParamCodeEnum.ADMISSION_TYPE_MANUAL.getCode().intValue()) {
            //准考证方式-手动
            return getAdmissionManual(taskId, paramMap);
        } else if (admissionType == ArrangeParamCodeEnum.ADMISSION_TYPE_RANDOM.getCode().intValue()) {
            //准考证方式-随机
            return getAdmissionRandom(taskId, paramMap);
        }
        return null;
    }


    /**
     * 获取准考证号-分组
     *
     * @param taskId
     * @param paramMap
     * @param taskPlaceVO
     * @return
     */
    private String getAdmissionGroup(String taskId, Map paramMap, TaskPlaceVO taskPlaceVO) {

        StringBuilder builder = new StringBuilder();
        String statusOn = "true";
        if (paramMap.get(StringCodeEnum.ARRANGE_PARAM_GROUP_YEAR.getCode()).equals(statusOn)) {
            int groupYearDigit = Integer.parseInt(String.valueOf(paramMap.get("groupYearDigit")));
            builder.append(DateTimeUtil.getFormatYear(groupYearDigit));
        }

        if (paramMap.get(StringCodeEnum.ARRANGE_PARAM_GROUP_PROVINCE.getCode()).equals(statusOn)) {
            int digit = Integer.parseInt(String.valueOf(paramMap.get("groupProvinceDigit")));
            builder.append(StringUtil.stringRight(taskPlaceVO.getProvince(), digit));
        }

        if (paramMap.get(StringCodeEnum.ARRANGE_PARAM_GROUP_CITY.getCode()).equals(statusOn)) {
            int digit = Integer.parseInt(String.valueOf(paramMap.get("groupCityDigit")));
            builder.append(StringUtil.stringRight(taskPlaceVO.getCity(), digit));
        }

        if (paramMap.get(StringCodeEnum.ARRANGE_PARAM_GROUP_DISTRICT.getCode()).equals(statusOn)) {
            int digit = Integer.parseInt(String.valueOf(paramMap.get("groupDistrictDigit")));
            builder.append(StringUtil.stringRight(taskPlaceVO.getDistrict(), digit));
        }

        String key = StringUtils.isBlank(builder.toString()) ? "NoArea" : builder.toString();

        if (paramMap.get(StringCodeEnum.ARRANGE_PARAM_GROUP_RANDOM.getCode()).equals(statusOn)) {
            int digit = Integer.parseInt(String.valueOf(paramMap.get("groupRandomDigit")));
            builder.append(StringUtil.randomNumber(digit));
        }

        if (paramMap.get(StringCodeEnum.ARRANGE_PARAM_GROUP_AUTO.getCode()).equals(statusOn)) {
            int digit = Integer.parseInt(String.valueOf(paramMap.get("groupAutoDigit")));

            int maxAuto = 0;
            if (paramMap.get(key) == null) {
                maxAuto = timeRoomCandidateMapper.selectMaxSeaNumByGroup(taskId, digit, taskPlaceVO.getProvince(), taskPlaceVO.getCity(), taskPlaceVO.getDistrict());
            } else {
                maxAuto = Integer.parseInt(String.valueOf(paramMap.get(key)));
            }

            builder.append(StringUtil.stringFormatZero((maxAuto + 1), digit));

            paramMap.put(key, maxAuto + 1);
        }

        return builder.toString();
    }

    /**
     * 获取准考证号-手动
     *
     * @param taskId
     * @param paramMap
     * @return
     */
    private String getAdmissionManual(String taskId, Map paramMap) {

        //1.获取自增的最大值
        int digit = Integer.parseInt(String.valueOf(paramMap.get("manualIncreaseDigit")));
        int max = 0;
        if (paramMap.get(StringCodeEnum.ARRANGE_PARAM_MANUAL_AUTO_MAX.getCode()) == null) {
            //查询数据库
            max = timeRoomCandidateMapper.selectMaxSeaNumByManual(taskId, digit);
        } else {
            max = Integer.parseInt(String.valueOf(paramMap.get("manualAutoMax")));
        }

        //获取随机数位数
        int randomNumberDigits = Integer.parseInt(paramMap.get(StringCodeEnum.ARRANGE_PARAM_RANDOM_NUMBER_DIGIT.getCode()).toString());

        //2.加一，并格式化补全0
        StringBuilder builder = new StringBuilder();
        //自定义前缀
        builder.append(paramMap.get("manual"));
        //随机数
        if (randomNumberDigits > 0) {
            builder.append(RandomUtil.randomNumbers(randomNumberDigits));
        }
        //自增序号
        builder.append(StringUtil.stringFormatZero((max + 1), digit));

        paramMap.put("manualAutoMax", (max + 1));
        return builder.toString();
    }

    /**
     * 生成准考证号-随机
     *
     * @param taskId
     * @param paramMap
     * @return
     */
    private String getAdmissionRandom(String taskId, Map paramMap) {
        int digit = Integer.parseInt(String.valueOf(paramMap.get("randomDigit")));

        List<String> list = (List<String>) paramMap.get("admissionNumber_random");
        if (list == null || list.isEmpty()) {
            list = timeRoomCandidateMapper.selectAllSeaNumByRandom(taskId);
        }

        if (list == null || list.isEmpty()) {
            list = new ArrayList<>();
        }

        String admissionNumber = null;
        String randomParam = String.valueOf(paramMap.get("randomParam"));
        while (StringUtils.isBlank(admissionNumber)) {
            admissionNumber = StringUtil.randomStringOrNumberNew(digit, randomParam);
            if (list.contains(admissionNumber)) {
                admissionNumber = null;
            }
        }

        list.add(admissionNumber);
        paramMap.put("admissionNumber_random", list);

        return admissionNumber;
    }


}
